package storage

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jonasngs/go_entry_task/tcp_server/config"
	"log"
)

type Database struct {
	DB *sql.DB
}

var dbConfig config.MySQL

func dsn(dbName string, username string, password string, hostname string) string {
	return fmt.Sprintf("%s:%s@tcp(%s)/%s", username, password, hostname, dbName)
}

func createDatabase(db *sql.DB) {
	createDBQuery := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8 COLLATE utf8_bin",
		dbConfig.DbName)
	if _, err := db.Exec(createDBQuery); err != nil {
		log.Printf("Error %s when opening DB\n", err)
	}

	if _, err := db.Exec("USE " + dbConfig.DbName); err != nil {
		log.Printf("Error %s when using DB\n", err)
	}

}

func configureDatabase(db *sql.DB) {
	db.SetMaxOpenConns(dbConfig.MaxConnections)
	db.SetMaxIdleConns(dbConfig.MaxIdleConnections)
	// db.SetConnMaxLifetime(time.Minute * 5)
}

func InitializeDatabase() Database {

	dbConfig = config.GetMYSQLServer()

	connectionURL := dsn(dbConfig.Driver, dbConfig.Username, dbConfig.Password, dbConfig.Hostname)
	db, err := sql.Open(dbConfig.Driver, connectionURL)
	if err != nil {
		log.Printf("Error %s when opening DB\n", err)
	}
	createDatabase(db)
	configureDatabase(db)
	return Database{DB: db}
}
